package com.example.mysimpleexample.fragment;

import android.app.Dialog;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;

import androidx.appcompat.app.AlertDialog;

import com.example.mysimpleexample.R;
import com.xuexiang.xpage.annotation.Page;
import com.xuexiang.xpage.base.XPageFragment;
import com.xuexiang.xui.widget.dialog.materialdialog.MaterialDialog;
import com.xuexiang.xui.widget.edittext.MultiLineEditText;
import com.xuexiang.xui.widget.popupwindow.easypopup.EasyPopup;
import com.xuexiang.xui.widget.textview.supertextview.SuperTextView;

import butterknife.BindView;

@Page(name = "编辑资料")
public class MyInfoFragment extends XPageFragment {
    @BindView(R.id.srv_header)
    SuperTextView srvHeader;
    private EasyPopup easyPopup;
    @BindView(R.id.srv_input_name)
    SuperTextView srvInputName;
    @BindView(R.id.srv_sex)
    SuperTextView srvSex;
    @BindView(R.id.srv_birthday)
    SuperTextView srvBirthday;
    @Override
    protected int getLayoutId() {
        return R.layout.fragment_my_info;
    }

    @Override
    protected void initViews() {

    }

    @Override
    protected void initListeners() {
        srvHeader.setOnSuperTextViewClickListener(new SuperTextView.OnSuperTextViewClickListener() {
            @Override
            public void onClick(SuperTextView superTextView) {
                AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
                View view = LayoutInflater.from(getContext()).inflate(R.layout.dialog_choose_header,null);
                TextView tvCancel = view.findViewById(R.id.tv_header_cancel);
                AlertDialog dialog = builder.create();
                dialog.show();
                dialog.getWindow().setContentView(view);
                tvCancel.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialog.dismiss();
                    }
                });
            }
        });
        srvInputName.setOnSuperTextViewClickListener(new SuperTextView.OnSuperTextViewClickListener() {
            @Override
            public void onClick(SuperTextView superTextView) {
                Dialog dialog = new Dialog(getContext(), R.style.dialog_bottom_full);
                dialog.setCanceledOnTouchOutside(true);
                dialog.setCancelable(true);
                Window window = dialog.getWindow();
                window.setGravity(Gravity.BOTTOM);
                window.setWindowAnimations(R.style.dialog_animation);
                View view = View.inflate(getContext(), R.layout.dialog_input_name, null);
                final Button btnCancel = view.findViewById(R.id.btnInputNameDialogCancel);
                final Button btnOK = view.findViewById(R.id.btnInputNameDialogOK);
                btnCancel.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (dialog != null && dialog.isShowing()) {
                            dialog.dismiss();
                        }
                    }
                });
                btnOK.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (dialog != null && dialog.isShowing()) {
                            dialog.dismiss();
                        }
                    }
                });
                final MultiLineEditText metInputName = view.findViewById(R.id.met_name);
                metInputName.setFocusable(true);
                metInputName.setFocusableInTouchMode(true);
                window.setContentView(view);
                window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);//设置横向全屏
                dialog.show();
            }
        });
        srvSex.setOnSuperTextViewClickListener(new SuperTextView.OnSuperTextViewClickListener() {
            @Override
            public void onClick(SuperTextView superTextView) {
                String[] sexItems = new String[]{"男","女"};
                MaterialDialog.Builder builder = new MaterialDialog.Builder(getContext());
                builder.title("选择性别");
                builder.items(sexItems);
                int selectedIndex = 0;
                String sex = srvSex.getRightTextView().getText().toString();
                if (sex.equals("男")) {
                    selectedIndex = 0;
                }else{
                    selectedIndex = 1;
                }
                builder.itemsCallbackSingleChoice(selectedIndex, new MaterialDialog.ListCallbackSingleChoice() {
                    @Override
                    public boolean onSelection(MaterialDialog dialog, View itemView, int which, CharSequence text) {
                        srvSex.getRightTextView().setText(text);
                        return true;
                    }
                });
                builder.positiveText("确定");
                builder.negativeText("取消");
                builder.show();
            }
        });
        srvBirthday.setOnSuperTextViewClickListener(new SuperTextView.OnSuperTextViewClickListener() {
            @Override
            public void onClick(SuperTextView superTextView) {
                AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
                AlertDialog dialog = builder.create();
                View view = LayoutInflater.from(getContext()).inflate(R.layout.dialog_input_birthday,null);
                DatePicker datePicker = view.findViewById(R.id.datePicker);
                CharSequence initDateStr = srvBirthday.getRightTextView().getText();
                Log.e("ss",initDateStr.toString());
                if (initDateStr != null) {
                    String year = initDateStr.toString().split("-")[0];
                    String month = initDateStr.toString().split("-")[1];
                    String day = initDateStr.toString().split("-")[2];
                    datePicker.init(Integer.parseInt(year),Integer.parseInt(month) - 1,Integer.parseInt(day),null);
                }
                TextView tvCancel = view.findViewById(R.id.tv_cancel);
                tvCancel.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (dialog != null && dialog.isShowing()) {
                            dialog.dismiss();
                        }
                    }
                });
                TextView tvOK = view.findViewById(R.id.tv_ok);
                tvOK.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        String dateStr = datePicker.getYear() + "-" + (datePicker.getMonth() + 1) + "-" + datePicker.getDayOfMonth();
                        srvBirthday.getRightTextView().setText(dateStr);
                        if (dialog != null && dialog.isShowing()) {
                            dialog.dismiss();
                        }
                    }
                });
                dialog.show();
                dialog.getWindow().setContentView(view);

            }
        });
    }
}
